/**
 * @Title: InitDataSyncLogic.java
 * @Package: com.jason.fingerprint.logic
 * @Descripton: 初始化应用信息
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月27日15:40:30
 * @Version: V1.0
 */
package com.jason.fingerprint.logic;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.google.gson.GsonBuilder;
import com.jason.fingerprint.AppContext;
import com.jason.fingerprint.AppGlobal;
import com.jason.fingerprint.beans.*;
import com.jason.fingerprint.common.UIHelper;
import com.jason.fingerprint.configs.HttpConfig;
import com.jason.fingerprint.utils.CodecUtils;
import org.kymjs.aframe.http.HttpCallBack;
import org.kymjs.aframe.http.KJFileParams;
import org.kymjs.aframe.http.KJHttp;

import java.util.List;

/**
 * @ClassName: InGetVersionSyncLogictDataSyncLogic
 * @Description: 版本更新
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-5-22 16:31:27
 */
public class GetVersionSyncLogic extends Logic {

	private static final String TAG = "GetVersionSyncLogic";
	private InitDataAsyncTask mInitDataAsyncTask;
	private Handler mHandler;
	private boolean mIsCallBack;// 是否有回调，此需要Handle
	private Message mMessage;
	private String mVersionName;

	public GetVersionSyncLogic(){
		super();
		this.mInitDataAsyncTask = new InitDataAsyncTask();
	}

	public GetVersionSyncLogic(AppContext appContext, Handler handler, boolean isCallBack){
		super(appContext);
		this.mInitDataAsyncTask = new InitDataAsyncTask();
		this.mIsCallBack = isCallBack;
		this.mHandler = handler;
		this.mMessage = new Message();
	}

	/*
	 * @see com.jason.fingerprint.logic.Logic#execute()
	 */
	@Override
	public void execute() {
//		mInitDataAsyncTask.execute();
		Context context = AppContext.getAppContext();
		try {
			PackageInfo info =  context.getPackageManager().getPackageInfo(context.getPackageName(),0);
			mVersionName = info.versionName;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		updateVersion();
	}

	private void updateVersion(){
		mParams = new KJFileParams();
		mParams.put(AppGlobal.HTTP_PARAM_IMEI, mImei);
		mParams.put("versionNum", mVersionName);
		String url = HttpConfig.POST_URL_VERSION_GETVERSION;
		Log.i(TAG, "----->API:" + url);
		Log.i(TAG, "----->mParams:" + mParams.getParamString());
		KJHttp kjHttp = new KJHttp();
		kjHttp.post(url, mParams, new HttpCallBack() {

			@Override
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onSuccess(Object arg0) {
				// TODO Auto-generated method stub
				String result = null;
				VersionResp versionResp = null;
				try{
					result = CodecUtils.base64Decode(arg0.toString());
					Log.i(TAG, "----->Result:" + result);
					versionResp = new GsonBuilder().create().fromJson(result,
							VersionResp.class);
				} catch (Exception e){
					Log.i(TAG,"updateVersion this exception message :" + e.getMessage());
				}
				if (versionResp != null && versionResp.isSuccess()) {
					List<VersionBean> beans = versionResp.getValues();
					if (beans != null && !beans.isEmpty()){
						mMessage.what = UIHelper.VERSION_SUCCESS;
						mMessage.obj = beans.get(0);
						send();
					}else {
						mMessage.what = UIHelper.VERSION_NO_DATA;
						send();
					}
				}else {
					mMessage.what = UIHelper.VERSION_FAIL;
					send();
				}

			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				mMessage.what = UIHelper.VERSION_FAIL;
				send();
			}

		});
	}


	/*
	 * @see com.jason.fingerprint.logic.Logic#send()
	 */
	@Override
	public void send() {
		if (mIsCallBack) {
			mHandler.sendMessage(mMessage);
			mMessage = new Message();
		}
	}


	private class InitDataAsyncTask extends AsyncTask<Void, String, String>{

		/*
		 * @see android.os.AsyncTask#doInBackground(Params[])
		 */
		@Override
		protected String doInBackground(Void... params) {
			// 运行在后台线程中
			
			return null;
		}
		
		/*
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(String result) {
			// TODO 自动生成的方法存根
			super.onPostExecute(result);
		}
		
	}


	/* (non-Javadoc)
	 * @see com.jason.fingerprint.logic.Logic#destory()
	 */
	@Override
	public void destory() {
		// TODO Auto-generated method stub
		
	}
}
